 package com.davydov.suabl.model;
 import java.awt.Image;
 import java.awt.event.KeyEvent;
 import com.davydov.suabl.Force;
 import com.davydov.suabl.Position;
 public class HeroCharacter extends Character {
	 public enum power {
		 air, porc, pellet, portal, fire, none
	 }
	 private power Power;
	 protected HeroCharacter(int health, int mass, Position pos, Image avatar) {
		 super(health, mass, pos, avatar);
		 this.Power = power.none;
	 }
 
	 public String getName() {
		 return "HeroCharacter";
	 }
	 @Override
	 void die(Environment env) {
		 // TODO Auto-generated method stub
		 env.remove(this);
	 }
	 @Override
	 void fire(Environment env) {
		 // TODO Auto-generated method stub
  
	 }
	 @Override
	 public void tick(Environment env) {
		 // TODO YOU NEED TO GET ME THE KEYS PRESSED SOMEHOW NOW THAT WE'RE NOT DOING THAT
		 //imposed air friction
		 if (this.getNetForce().getDirection() == 0 && this.getNetForce().getMagnitude() > 0)
		 {
			 this.setNetForce(new Force(0, this.getNetForce().getMagnitude() - .2));
		 }
		 else if (this.getNetForce().getDirection() == Math.PI && this.getNetForce().getMagnitude() > 0)
		 {
			 this.setNetForce(new Force(Math.PI, this.getNetForce().getMagnitude() - .2));
		 }
  
		 boolean left = false;
		 boolean right = false;
		 boolean up = false;
		 boolean fire = false;
		 //not really sure how we're gonna handle keysPressed; gotta either be passed in, I know that physics engine now has keysPressed method, but not implemented
		 for (int i = 0; i < keysPressed.length; i++)
		 {
			 if (keysPressed[i] == KeyEvent.VK_LEFT)
			 {
				 left = true;
			 }
			 else if (keysPressed[i] == KeyEvent.VK_RIGHT)
			 {
				 right = true;
			 }
			 else if(keysPressed[i] == KeyEvent.VK_UP)
			 {
				 up = true;
			 }
			 else if (keysPressed[i] == KeyEvent.VK_SPACE)
			 {
				 fire = true;
			 }
		 }
		 if (left && !right)
		 {
			 this.setNetForce(new Force(Math.PI,1));
		 }
		 if (right && !left)
		 {
			 this.setNetForce(new Force(0,1));
		 }
		 if (up)
		 {
			 double x = this.getNetForce().getMagnitude() * Math.cos(this.getNetForce().getDirection());
			 double y = this.getNetForce().getMagnitude() * Math.sin(this.getNetForce().getDirection());
   
			 y = y + 10;
			 double tentativeDirection = Math.abs(Math.atan(Math.abs(y)/Math.abs(x)));
			 double realDirection = 0;
			 //if the affector's x is greater than obj's x and the same is true for y's, then it's in the first quadrant
			 if ( x > 0 && y < 0)
			 {realDirection = tentativeDirection;}
			 //if the affector's x is greater than the obj's x, but the affector's y is less than the obj's y it's in the 4th quadrant
			 else if( x > 0 && y < 0)
			 {realDirection = (2 * Math.PI) - tentativeDirection;}
			 //if the affector's x is less than the object's, and the same is true about the y, then it's in the 3rd quadrant
			 else if (x < 0 && y > 0)
			 {realDirection = Math.PI + tentativeDirection;}
			 //otherwise it'd be in the 2nd quadrant, but just for good measure, I'll define the condition since I probably messed up somewhere
			 else if (x < 0 && y < 0)
			 {realDirection = Math.PI - tentativeDirection;}
			 //this is the final force
			 Force iHateThis = new Force(realDirection,Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))); //Are you freakin' kiddin' me, this better work.
			 this.setNetForce(iHateThis);
		 }
  
		 if (fire)
		 {
			 if (Power == power.none)
			 {
				 for(int z = 0; z < env.getAllObjects().size(); z++) {
					 if (env.getAllObjects().get(z) instanceof Character)
					 {
						 if (this.inTriangle(this.getPosition().getX(), this.getPosition().getY(), this.getPosition().getX()+100, this.getPosition().getY()+20, this.getPosition().getX()+100, this.getPosition().getY()-20, env.getElements().get(z).getPos().getX(), env.getElements().get(z).getPos().getY()))
						 {
							 if (env.getAllObjects().get(z) instanceof AirGunPatrol)
							 {
								 Power = power.air;
							 }
							 else if (env.getAllObjects().get(z) instanceof ElectricalPorcupineMonster)
							 {
								 Power = power.porc;
							 }
							 else if (env.getAllObjects().get(z) instanceof FireDragonMonster)
							 {
								 Power = power.fire;
							 }
							 //kill swallowed monster
							 env.remove(env.getAllObjects().get(z));
						 }
					 }
				 }
			 }
		 }
	 }
}
